﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.ActionFilterAttributes;

namespace DLULearningNetwork.Controllers
{
    public class AdminCategoriesController : Controller
    {
        //
        // GET: /AdminCategories/
        [RequireAdminLogin()]
        public ActionResult Index()
        {
            int totalItem = 0;
            List<Category> lstCategory = Category.GetAllCategory(1, Libs.Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItem);
            ViewBag.TotalItems = totalItem;
            ViewBag.CurrentPage = 1;
            return View(lstCategory);
        }
        [HttpPost]
        [RequireAdminLogin()]
        public ActionResult GetListCategory(int page)
        {
            int totalItem = 0;
            List<Category> lstCategory = Category.GetAllCategory(page, Libs.Constants.DEFAULT_NUMBER_OF_ITEMS_PERPAGE, ref totalItem);
            ViewBag.TotalItems = totalItem;
            ViewBag.CurrentPage = page;
            return View("ListCategory", lstCategory);
        }

        [RequireAdminLogin()]
        [HttpPost]
        public ActionResult CreateNewCategory(string categoryName)
        {
            if (categoryName.Length < 5 || categoryName.Length > 50)
            { 
                return PartialView("ErrorMessage","unsuccess/3");
            }
            Category category = new Category();
            category.CategoryID_u = Guid.NewGuid();
            category.Name_s = categoryName;
            int kq = Category.Add(category);
            if (kq == 1)
            {
                return PartialView("CategoryRow", category);
            }
            else
            {
                if (kq == 2)
                {
                    return PartialView("ErrorMessage","unsuccess/2");
                }
                return PartialView("ErrorMessage", "unsuccess");
            }
        }

        [RequireAdminLogin()]
        [HttpPost]
        public string DeleteCategory(Guid categoryID)
        {
            int kq = Category.DeleteByID(categoryID);
            if (kq == 1)
            {
                return "success";
            }
            else
            {
                if (kq == 2)
                {
                    return "unsuccess/2";
                }
                return "unsuccess";
            }
        }

        [RequireAdminLogin()]
        [HttpPost]
        public string UpdateCategory(Guid categoryID, string categoryName)
        {
            if (categoryName.Length < 5 || categoryName.Length > 50)
            {
                return  "unsuccess/3";
            }
            int kq = Category.UpdateByID(categoryID, categoryName);
            if (kq == 1)
            {
                return "success";
            }
            return "unsuccess";
        }

    }
}
